<template>
  <div>
    <brack :params1="'权限管理'" :params2="'权限列表'"></brack>
    <el-card class="box-card">
      <el-row>
        <el-col :span="8"
          ><el-input placeholder="请输入内容" class="input-with-select"> <el-button slot="append" icon="el-icon-search"></el-button> </el-input
        ></el-col>
        <el-col :span="12"><el-button type="primary" class="btn" @click="addGoods">添加用户</el-button></el-col>
      </el-row>
      <!-- 表单区域 -->
      <template>
        <el-table :data="list" border style="width: 100%">
          <el-table-column type="index" label="#" width="50"> </el-table-column>
          <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
          <el-table-column prop="goods_price" label="商品价格(元)" width="150"> </el-table-column>
          <el-table-column prop="goods_weight" label="商品重量" width="80"> </el-table-column>
          <el-table-column prop="upd_time" label="创建时间 " width="150"> </el-table-column>

          <el-table-column prop="address" label="操作" width="150">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>

              <el-button @click="remove(scope.row)" size="mini" type="danger" icon="el-icon-delete"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>

      <!-- 分页区域 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { goods_api, removegoods_api } from '@/api'
import dayjs from 'dayjs'
export default {
  data() {
    return {
      list: [],
      total: 1,
      obj: {
        query: '1',
        pagenum: 1,
        pagesize: 10
      },
      currentPage4: 1
    }
  },
  created() {
    this.goods()
  },
  methods: {
    async goods() {
      const { data: res } = await goods_api(this.obj)
      // console.log(res, 1111);
      this.list = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.obj.pagesize = val
      this.goods()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.obj.pagenum = val
      this.goods()
    },
    // 删除按钮
    remove(row) {
      console.log(row.goods_id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const { data: res } = removegoods_api(row.goods_id)
          // 没有返回值 重新渲染页面
          this.goods()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //  点击跳转添加商品页面
    addGoods() {
      this.$router.push('goodsadd')
    }
  }
}
</script>
<style scoped lang="less">
.box-card {
  margin-top: 20px;
  .el-row {
    margin-bottom: 20px;
    .btn {
      margin-left: 20px;
    }
  }
  .el-table__body-wrapper {
    tbody {
      .el-table__row {
        .cell {
          padding-left: none;
        }
      }
    }
  }
}
</style>
